package com.luwak.controller.sys;

import com.luwak.common.annotation.SysLog;
import com.luwak.common.base.Constant;
import com.luwak.common.base.R;
import com.luwak.common.utils.PageUtils;
import com.luwak.common.validator.ValidatorUtils;
import com.luwak.common.validator.group.AddGroup;
import com.luwak.common.validator.group.UpdateGroup;
import com.luwak.modules.sys.entity.SysUserEntity;
import com.luwak.modules.sys.service.SysUserRoleService;
import com.luwak.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ChenXc
 * @version V1.0
 * @Date 2018/3/26  10:04
 * @Description TODO(用户)
 */
@Api(value = "系统controller", description = "用户", tags = {"用户操作接口"})
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;


    /**
     * 所有用户列表
     */
    @ApiOperation("查询所有用户")
    @GetMapping("/list")
    @RequiresPermissions("sys:user:list")
    public R list(@RequestParam @ApiParam(name = "查询参数", value = "传入json格式") Map<String, Object> params) {
        //只有超级管理员，才能查看所有管理员列表
        if (getUserId() != Constant.SUPER_ADMIN) {
            params.put("createUserId", getUserId());
        }
        PageUtils page = sysUserService.queryPage(params);

        return R.ok().put("page", page);
    }


    @ApiOperation("保存用户")
    @SysLog("保存用户")
    @PostMapping("/save")
    @RequiresPermissions("sys:user:save")
    public R save(@RequestBody SysUserEntity user) {

        ValidatorUtils.validateEntity(user, AddGroup.class);

        user.setCreateUserId(getUserId());

        sysUserService.save(user);

        return R.ok();
    }

    /**
     * 用户信息
     */
    @ApiOperation("查询用户信息")
    @GetMapping("/info/{userId}")
    @RequiresPermissions("sys:user:info")
    public R info(@PathVariable("userId") @ApiParam(name = "查询参数", value = "传入json格式", required = true) Long userId) {

        SysUserEntity user = sysUserService.selectById(userId);

        //获取用户所属的角色列表
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);

        return R.ok().put("data", user);
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @PostMapping("/update")
    @RequiresPermissions("sys:user:update")
    public R update(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, UpdateGroup.class);

        user.setCreateUserId(getUserId());
        sysUserService.update(user);

        return R.ok();
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @PostMapping("/delete")
    @RequiresPermissions("sys:user:delete")
    public R delete(@RequestBody Long[] userIds) {
        if (ArrayUtils.contains(userIds, 1L)) {
            return R.error("系统管理员不能删除");
        }

        if (ArrayUtils.contains(userIds, getUserId())) {
            return R.error("当前用户不能删除");
        }

        sysUserService.deleteBatch(userIds);

        return R.ok();
    }

    /**
     * 获取登录的用户信息
     */
    @GetMapping("/info")
    @ApiOperation("获取登录的用户信息")
    public R info() {
        return R.ok().put("data", getUser());
    }
}
